<?php
namespace Common\Model;
use Think\Model;
	
class CustomAgreementModel extends Model{
	public $pagesize = 10;

	/**
	 * 获取合同名称
	 * @param  [type] $agreement_id 合同ID
	 */
	public function get_name($agreement_id, $separator = ','){
		$where['id'] = array('in', $agreement_id);
		$agreement_name_arr = $this->where($where)->getField('title', true);
		$agreement_name_str = implode($separator, $agreement_name_arr);

		return $agreement_name_str ? $agreement_name_str : '';
	}
	/**
	 * 获取类别下合同清单
	 * @param  string  $module 类别标识
	 * @param  integer $num    数量
	 */
	public function get_sort_agreement_list($module = 'agreement', $num = 10){
		$sort_list = D('Category')->get_list($module);
		foreach ($sort_list as $rs) {
			$rs['list'] = $this->get_list(array('category_id' => $rs['id']), '', 1, $num);
			$new_list[] = $rs;
		}
		return $new_list;
	}
	/**
	 * 合同详情
	 * @param  [type] $agreement_id 合同ID
	 */
	public function info($agreement_id){
		$where['id']        = $agreement_id;
		$where['status']    = 1;
		$where['is_delete'] = 0;
		$info = $this->where($where)->find();
		$info = $this->filter_field($info);
		return $info;
	}
	public function get_list($where = '', $order = '', $page = 1, $pagesize = 0){
		$page      = $page ? $page : 1;
		$pagesize  = $pagesize ? $pagesize : $this->pagesize;
		$first_row = $pagesize * ($page - 1);

		//默认排序
		if(!$order){
			$order['order_id']  = 'asc';
			$order['edit_time'] = 'desc';
			$order['id']        = 'desc';
		}

		$where['status']    = 1;
		$where['is_delete'] = 0;
		$count = $this->where($where)->count();
		$list  = $this->where($where)->limit($first_row . ',' . $pagesize)->order($order)->select();
		foreach ($list as $rs) {
			$new_list[] = $this->filter_field($rs);
		}
		$this->count = $count;
		return $new_list ? $new_list : array();
	}

	public function filter_field($data){
		$images_id_arr = explode(',', $data['images_ids']);
		$images_id     = empty($images_id_arr) ? 0 : $images_id_arr[0];
		$data['agreement_id']      = $data['id'];
		$data['date_desc']         = date('Y-m-d', $data['edit_time']);
		$data['time_desc']         = time_ago_desc($data['edit_time']);
		$data['datetime_desc']     = date('Y-m-d H:i:s', $data['edit_time']);
		$data['images_url']        = D('File')->get_path($images_id);
		$data['icon_url']          = D('File')->get_path($data['icon_id']);
		$data['selected_icon_url'] = D('File')->get_path($data['selected_icon_id']);
		$data['category_name']     = D('Category')->get_name($data['category_id']);

		$data['service_content'] = preg_replace("/title=\".*?\"/si", "", $data['service_content']); //过滤自定义样式
		$data['why_select_milv'] = preg_replace("/title=\".*?\"/si", "", $data['why_select_milv']); //过滤自定义样式
		unset($data['id']);
		return $data;
	}
	/**
	 * 更新下订单人数
	 * @param  [type] $agreement_id 合同ID
	 */
	public function update_order_num($agreement_id){
		$where['id'] = $agreement_id;
		$this->where($where)->setInc('order_num');
	}
	/**
	 * 更新查看人数
	 * @param  [type] $agreement_id 合同ID
	 */
	public function update_view_num($agreement_id){
		$where['id'] = $agreement_id;
		$this->where($where)->setInc('view_num');
	}
}